package com.somsoft.handjc.activity.query.xzsp;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;

import com.somsoft.handjc.activity.R;
import com.somsoft.handjc.activity.jiancha.xzsp.XzspLjjcListActivity;
import com.somsoft.handjc.activity.jiancha.xzsp.XzspSxjcListActivity;
import com.somsoft.handjc.util.BaseActivity;
import com.somsoft.handjc.util.MyApplication;
import com.somsoft.handjc.util.XzspDataUtil;

//行政审批 综合查询
public class XzspQueryActivity extends BaseActivity {
	private Button btnSubmit = null;
	private Button btnClear = null;
	private Button btnReset = null;
	private String from = "";
	
	private EditText deptName = null;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		MyApplication.getInstance().addActivity(this);
		setContentView(R.layout.xzsp_zhonghe_query);
		setTitle("行政审批综合查询条件");
		btnSubmit = (Button)findViewById(R.id.btnSubmit);
		btnClear = (Button)findViewById(R.id.btnClear);
		btnReset = (Button)findViewById(R.id.btnReset);
		deptName = (EditText)findViewById(R.id.deptName);
		
		btnSubmit.setOnClickListener(new OnClickListenerImpl());
		btnClear.setOnClickListener(new OnClickListenerImpl());
		btnReset.setOnClickListener(new OnClickListenerImpl());
		deptName.setOnClickListener(new OnClickListenerImpl());
		deptName.setOnFocusChangeListener(new OnFocusChangeListenerImpl());
		
		Intent it = getIntent();
		from = it.getStringExtra("from");
		if(from == null){
			from = "";
		}
	}
	
	private class OnFocusChangeListenerImpl implements OnFocusChangeListener{

		@Override
		public void onFocusChange(View v, boolean hasFocus) {
			if(hasFocus){
				int id = v.getId();
				if(id == R.id.deptName){
					//责任单位
					ListAdapter adapter = new ArrayAdapter<String>(XzspQueryActivity.this, R.layout.simple_list_item_2,getDeptData()); 
					AlertDialog.Builder builder = new AlertDialog.Builder(XzspQueryActivity.this);
					builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							deptName.setText(XzspDataUtil.xzspDeptNames.get(which));
						}
					});
					builder.setNegativeButton("取消",new DialogInterface.OnClickListener(){
						@Override
						public void onClick(DialogInterface dialog, int which) {
							try {
								dialog.dismiss();
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					});
					AlertDialog alertDialog = builder.create();
					alertDialog.show();
					WindowManager.LayoutParams  lp= alertDialog.getWindow().getAttributes();
				    lp.height=900;//定义高度
				    alertDialog.getWindow().setAttributes(lp);
				}
			}
			
		}
		
	}
	
	private class OnClickListenerImpl implements OnClickListener{

		@Override
		public void onClick(View v) {
//			Button btn = (Button)v;
			int id = v.getId();
			
			EditText yxtywlsh = (EditText)findViewById(R.id.yxtywlsh);
			EditText projectName = (EditText)findViewById(R.id.projectName);
			
			Intent it = null;
			if("sxcx".equals(from)){
				it = new Intent(XzspQueryActivity.this,XzspSxQueryListActivity.class);
			}else if("ljcx".equals(from)){
				it = new Intent(XzspQueryActivity.this,XzspLjQueryListActivity.class);
			}else if("yccx".equals(from)){
				it = new Intent(XzspQueryActivity.this,XzspYcQueryListActivity.class);
			}else if("sxjc".equals(from)){
				it = new Intent(XzspQueryActivity.this,XzspSxjcListActivity.class);
			}else if("ljjc".equals(from)){
				it = new Intent(XzspQueryActivity.this,XzspLjjcListActivity.class);
			}
			if(id == R.id.btnSubmit){
				it.putExtra("yxtywlsh", yxtywlsh.getText().toString());
				it.putExtra("projectName", projectName.getText().toString());
				it.putExtra("deptName", deptName.getText().toString());
				startActivity(it);
			}else if(id == R.id.btnClear){
				yxtywlsh.setText("");
				projectName.setText("");
			}else if(id == R.id.btnReset){
				startActivity(it);
			}else if(id == R.id.deptName){
				ListAdapter adapter = new ArrayAdapter<String>(XzspQueryActivity.this, R.layout.simple_list_item_2,getDeptData()); 
				AlertDialog.Builder builder = new AlertDialog.Builder(XzspQueryActivity.this);
				builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						deptName.setText(XzspDataUtil.xzspDeptNames.get(which));
					}
				});
				builder.setNegativeButton("取消",new DialogInterface.OnClickListener(){
					@Override
					public void onClick(DialogInterface dialog, int which) {
						try {
							dialog.dismiss();
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				});
				AlertDialog alertDialog = builder.create();
				alertDialog.show();
				WindowManager.LayoutParams  lp= alertDialog.getWindow().getAttributes();
			    lp.height=900;//定义高度
			    alertDialog.getWindow().setAttributes(lp);
			}
		}
	}
	
	private List<String> getDeptData(){ 
        List<String> data = new ArrayList<String>(); 
        if(XzspDataUtil.xzspDeptNames != null){
        	data = XzspDataUtil.xzspDeptNames;
        }
        return data; 
    }
}